<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>手机号登录</h1>
      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <el-form ref="myForm" size="medium" :model="formData" :rules="rules">
          <el-form-item prop="mobile">
            <el-input v-model="formData.mobile" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="formData.password" placeholder="请输入密码" show-password />
          </el-form-item>
          <el-form-item prop="agree">
            <el-checkbox v-model="formData.agree">用户平台使用协议</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width:350px" @click="onLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
/*
  完成表单校验功能
   1 data提供formData数据 el-form :model
   2 data提供rules 校验规则 el-form :rules
   3 el-form-item prop绑定rules校验规则
   4 el-input v-model双向绑定formData数据
   5 勾选按钮的校验 需要提供自定义规则校验
     因为required是必填 只能校验undefined null ''  无法校验布尔值
     所以需要用validator函数 进行自定义校验
      validator(rule, value, callback) {}
      rule 表示配置的规则对象 value是v-model收集的值 callback() 表示验证通过
      callback(new Error('验证失败提示信息'))
   6 点击登录按钮 调用form表单的方法 校验表单
      给表单添加ref属性
      给登录按钮添加点击事件 通过ref获取表单示例 调用表单的validate方法
      validate方法 返回的是promise 使用async await
    7 登录发送请求 存储token到vuex/user模块
      定义state 存放token
      定义mutation修改state的token
      定义action调用登录请求api/commit调用mutation方法赋值修改token
            发送请求 成功 存入token 提示用户 跳转路由
                     失败 拦截器统一处理 提示用户
    8 登录组件 调用action
 */
export default {
  name: 'Login',
  data() {
    return {
      formData: {
        mobile: '13800000002',
        password: 'hm#qd@23!',
        agree: true
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码6-16位', trigger: 'blur' }
        ],
        agree: [
          {
            validator(rule, value, callback) {
              if (value) {
                return callback()
              }
              callback(new Error('请勾选协议'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions('user', ['loginAction']),
    async onLogin() {
      await this.$refs.myForm.validate()
      await this.loginAction(this.formData)
      console.log('验证通过了，发送登录请求')
    }
  }
}
</script>
<style lang="scss">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/common/login_back.png)
      no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
    .icon {
      background: url(../../assets/common/logo.png) no-repeat 70px center /
        contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }
    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;
    .el-card {
      border: none;
      padding: 0;
    }
    h1 {
      padding-left: 20px;
      font-size: 24px;
    }
    .el-input {
      width: 350px;
      height: 44px;
      .el-input__inner {
        background: #f4f5fb;
      }
    }
    .el-checkbox {
      color:#606266;
    }
  }
}
</style>
